#!/bin/bash
if [ $# -lt 1 ];then
  echo "Usage: $0 [txtWordlist]"
  exit 1
fi

wordlst=$1
eachIndex=0
POSTFIX_URL="--_us_1.mp3"
TEMPLATE_URL="https://ssl.gstatic.com/dictionary/static/sounds/oxford/"
POSTFIX_YOUDAO="&le=eng"
TEMPLATE_YOUDAO="http://dict.youdao.com/dictvoice?audio="

#while read iw
echo -e "wordlist==>mp4:"
if [[ -d ${wordlst%.*} ]]; then
  rm -rf ${wordlst%.*}
fi

mkdir ${wordlst%.*}
gsed -i "s/ /+/g" ${wordlst}
for iw in $(cat ${wordlst})
do
  echo "${iw}"
  if [[ ${iw} =~ "+" ]];then
    MP3URL="${TEMPLATE_YOUDAO}${iw}${POSTFIX_YOUDAO}"
  elif [[ ${iw} =~ "http" ]];then
    curl --connect-timeout 10 -m 20 "${iw}" --output ${wordlst%.*}/httpCover.jpg
    ffmpeg -hide_banner -i ${wordlst%.*}/httpCover.jpg -vf pad=1280:720:640-iw/2:360-ih/2 -y ${wordlst%.*}/cover.jpg 2>/dev/null 1>/dev/null
    #ffmpeg -hide_banner -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 1 -acodec mp3 -y "curlwords/prefix.mp3" 2>/dev/null 1>/dev/null
    #ffmpeg -hide_banner -f image2 -loop 1 -i ${wordlst%.*}/cover.jpg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i "curlwords/prefix.mp3" -filter_complex amix=inputs=2:duration=longest -t 1 -acodec aac -ar 32000 -ac 2  -pix_fmt yuvj420p -vcodec libx264 -s 1280*720 -r 20 -t 1 -map 0:a:0 -map 0:v:0 -y -f mp4 "${wordlst%.*}/prefix.mp4" 2>/dev/null 1>/dev/null
    ffmpeg -hide_banner -f image2 -loop 1 -i ${wordlst%.*}/cover.jpg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 1 -acodec aac -ar 32000 -ac 2 -pix_fmt yuvj420p -vcodec libx264 -s 1280*720 -r 20 -t 1 -map 1:a:0 -map 0:v:0 -y -f mp4 "${wordlst%.*}/prefix.mp4" 2>/dev/null 1>/dev/null
    continue
  else
    MP3URL="${TEMPLATE_URL}${iw}${POSTFIX_URL}"
  fi

  let eachIndex=eachIndex+1
  #if [ ! -f "curlwords/${iw}.txt" ];then
    python3 req-youdao.py ${iw} > "curlwords/${iw}.txt"
    gsed -i "s/(' //g;s/ ')//g;s/'//g;s/ $//g;s/^ //g;s/；/\n/3;/^$/d" "curlwords/${iw}.txt"
    sleep 1 #sleep for crawler
  #fi

  if [ -f curlwords/${iw}.txt ];then
    inTxt=$(cat "curlwords/${iw}.txt")
    python3 pytext2jpg.py "${inTxt}" "curlwords/${iw}.jpg"
    echo "$inTxt"
  fi

  if [ ! -f "curlwords/${iw}.mp3" ];then
    curl --connect-timeout 10 -m 20 "${MP3URL}" --output "curlwords/${iw}.mp3"
    sleep 1 #sleep for crawler
  fi

  ffmpeg -hide_banner -f image2 -loop 1 -i "curlwords/${iw}.jpg" -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i "curlwords/${iw}.mp3" -filter_complex amix=inputs=2:duration=longest -t 2 -acodec aac -ar 32000 -ac 2 -pix_fmt yuvj420p -vcodec libx264 -s 1280*720 -r 20 -t 2 -y -f mp4 "${wordlst%.*}/w${eachIndex}.mp4" 2>/dev/null 1>/dev/null
done

cat /dev/null > mp4merge.list
if [ -f ${wordlst%.*}/prefix.mp4 ];then
  #ffmpeg -hide_banner -i ${wordlst%.*}/prefix.mp4 -acodec copy -vcodec copy -map 0:a:0 -map 0:v:0 -y ${wordlst%.*}/p0.mp4 2>/dev/null 1>/dev/null
  echo "file ${wordlst%.*}/prefix.mp4" >> mp4merge.list
fi

for ((each=1;each<=${eachIndex};each++))
do
  if [ -f ${wordlst%.*}/w${each}.mp4 ];then
    echo "file ${wordlst%.*}/w${each}.mp4"
  fi
done>>mp4merge.list
ffmpeg -hide_banner -f concat -safe 0 -i mp4merge.list -c copy -y "${wordlst%.*}_list.mp4"
exit 0

